package com.lonty.intelligence.service;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.mapper.BuildingMapper;
import com.lonty.intelligence.mapper.HouseholdMapper;
import com.lonty.intelligence.mapper.OwnerMapper;
import com.lonty.intelligence.model.Household;
import com.lonty.intelligence.model.Owner;
import com.lonty.intelligence.request.BuildingInfo;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.HouseholdModifyVo;
import com.lonty.intelligence.vo.HouseholdVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author 陈璇
 * @Date 2020/4/17 10:53
 * @Version 1.0
 */
@Service
public class HouseholdService {

    @Resource
    private HouseholdMapper householdMapper;

    @Resource
    private OwnerMapper ownerMapper;

    @Resource
    private BuildingMapper buildingMapper;

    /**
     * 获取所有的住户信息
     * @return
     */
    @Transactional
    public BuildingInfo getHouseholdInfoList(String buildingId){
        BuildingInfo buildingInfo = buildingMapper.getBuildingInfo(buildingId);
        buildingInfo.setHouseholdList(householdMapper.getByBuildingId(buildingId));
        return buildingInfo;
    }

    /**
     * 保存住户
     * @param householdVo
     * @return
     */
    @Transactional
    public Result saveHousehold(HouseholdVo householdVo){
        //判断是否为业主
        if(householdVo.getType().equals(1)){
            //判断业主是否存在
            Owner getOwner = ownerMapper.getByBuildingId(householdVo.getBuildingId());
            if(getOwner != null){
                return Result.builder(101, "业主已存在").build();
            }
            Owner owner = new Owner();
            BeanUtils.copyProperties(householdVo, owner);
            owner.setId(UUIDUtil.randomID());
            owner.setIsDel(0);
            ownerMapper.saveOwner(owner);
            //判断是否居住 居住则继续执行，不居住则跳出
//            if(householdVo.getIsLiving().equals(0)){
//                return Result.builder().data(owner).build();
//            }
        }
        Household household = new Household();
        BeanUtils.copyProperties(householdVo,household);
        household.setId(UUIDUtil.randomID());
        household.setIsDel(0);
        if(householdMapper.saveHousehold(household) > 0){
            return Result.builder().data(household).build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    /**
     * 根据ID修改姓名和手机号
     * @param householdModifyVo
     * @return
     */
    @Transactional
    public boolean modifyNameAndTelephone(HouseholdModifyVo householdModifyVo){
        Household household = new Household();
        BeanUtils.copyProperties(householdModifyVo, household);
        //根据ID获取住户
        Household getHousehold = householdMapper.getById(household.getId());
        //判断是否为业主 是则修改业主信息
        if(getHousehold.getType().equals(1)){
            ownerMapper.modifyOwnerInfo(getHousehold.getBuildingId(), householdModifyVo.getName(),
                    householdModifyVo.getTelephone(), householdModifyVo.getIsLiving());
        }
        return householdMapper.modifyHousehold(household) > 0;
    }

    /**
     * 删除住户
     * @param id
     * @return
     */
    public boolean deleteHousehold(String id){
        return householdMapper.deleteHousehold(id) > 0;
    }

}
